<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022/8/14
  Time: 8:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <base href="${pageContext.request.contextPath}/">
    <title>办公楼门禁管理系统</title>
    <style>
      table tr:nth-child(odd){
        background: lightgray;
      }
      input{
        margin-left: 535px;
      }
    </style>
    <script src="jq/jquery.js"></script>
  </head>
  <body>
    <h1 align="center">办公楼门禁管理系统</h1>
    <form action="add.jsp" method="post">
      <input type="submit" value="办卡">
      <table align="center">
        <tr style="background: darkgray" align="center">
          <th>持卡人姓名</th>
          <th>持卡人身份证号</th>
          <th>启用状态</th>
          <th>办卡日期</th>
          <th>操作</th>
        </tr>
        <c:forEach items="${sessionScope.list}" var="card">
          <tr>
            <td>${card.userName}</td>
            <td>${card.idNumber}</td>
            <c:choose>
              <c:when test="${card.status==0}">
                <td>禁用</td>
              </c:when>
              <c:otherwise>
                <td>启用</td>
              </c:otherwise>
            </c:choose>
            <td>${card.createDate}</td>
            <c:choose>
              <c:when test="${card.status==0}">
                <td><a href="javascript:void(0)" onclick="statusUpdate('${card.status}','${card.id}')" >启用</a></td>
              </c:when>
              <c:otherwise>
                <td><a href="javascript:void(0)" onclick="statusUpdate('${card.status}','${card.id}')" >禁用</a></td>
              </c:otherwise>
            </c:choose>
          </tr>
        </c:forEach>
      </table>
      <div align="center"><span id="span"></span></div>
    </form>
    <script>
      function statusUpdate(status,id) {
        $.getJSON(
                "CardUpdate",
                {status:status,id:id},
                function (data) {
                  if(data){
                    if(status==1){
                      $('#span').text('禁用成功');
                    }else {
                      $('#span').text('启用成功');
                    }
                    location.href="CardList";
                  }
                }
        )
      }
      $(function () {
        if(${sessionScope.msg==true}){
          alert("添加成功");
        }else if(${sessionScope.msg==false}){
          alert("添加失败");
        }
        ${sessionScope.remove("msg")}
      })
    </script>
  </body>
</html>
